package logic

import (
	"context"
	"fmt"
	"thing/model/cache"
	"thing/model/mysql"

	"thing/thingrpc/internal/svc"
	"thing/thingrpc/thingrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsAddLogic {
	return &GoodsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsAddLogic) GoodsAdd(in *thingrpc.GoodsAddRequest) (*thingrpc.GoodsAddResponse, error) {
	// todo: add your logic here and delete this line
	goods := mysql.Goods{
		Name:   in.Name,
		Price:  float64(in.Price),
		Image:  in.Image,
		Stock:  int(in.Stock),
		Status: int(in.Status),
	}
	err := goods.GoodsAdd()
	if err != nil {
		return nil, fmt.Errorf("商品添加失败")
	}
	cache.StockAdd(int(goods.ID), goods.Stock)
	return &thingrpc.GoodsAddResponse{
		GoodsId: int64(goods.ID),
	}, nil
}
